<!DOCTYPE html>
<html lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
<meta name="keywords" content="LightYear,LightYearAdmin,光年,后台模板,后台管理系统,光年HTML模板">
<meta name="description" content="Light Year Admin V5是一个基于Bootstrap v5.1.3的后台管理系统的HTML模板。">
<title>自动完成插件jquery.auto.complete - 光年(Light Year Admin V4)后台管理系统模板</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="default">
<link rel="stylesheet" type="text/css" href="css/materialdesignicons.min.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link href="js/jquery-auto-complete/jquery.auto-complete.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="css/style.min.css">
<style>

</style>
</head>
  
<body>
<div class="container-fluid">

  <div class="row">

  	<div class="col-lg-12">
  	  <div class="card">
  	    <header class="card-header">
  	      <div class="card-title">自动完成插件 jquery.auto.complete.js</div>
  	    </header>
  		<div class="card-body">
          
  		  <p>这里示例使用的版本是v1.0.7，因为涉及到后端接口返回查询，这里演示只返回同样的数据，请尝试输入"李"。</p>
          <form action="#!" method="post" id="example-from">
            <div class="mb-3">
              <label for="author" class="form-label">作者</label>
              <input type="text" class="form-control" placeholder="作者" name="example-author" id="author">
              <input type="hidden" name="author_id" id="author_id">
            </div>
          </form>
          
  	    </div>
  	  </div>
    </div>

  </div>

</div>

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/popper.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/jquery-auto-complete/jquery.auto-complete.js"></script>
<script type="text/javascript" src="js/main.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    //调用方法以设置参数并绑定数据
    $('#author').autoComplete({
        minChars: 1,
        source: function(term, suggest) {
            term = term.toLowerCase();
            
			$.ajax({  
				url : "./data/lyear_author.php", // 演示用，后端返回数据跟搜索内容无关（或者搜素'李'）
				type : "post",  
				dataType : "json",
				data : {"author" : term},  
				
				success: function( data ) {
					suggest(data);
				}  
			});
        },
        renderItem: function (item, search) {
            search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
            var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi");
            return '<div class="autocomplete-suggestion" data-id="'+item.id+'" data-val="'+search+'" data-result="'+item.name+'">'+item.name.replace(re, "<b>$1</b>")+'</div>';
        },
        onSelect: function(e, term, item){
            $("#author").val(item.data('result'));
            $("#author_id").val(item.data('id'));
        }
    });
    // 删除内容时将author_id置为空
    $("#author").on('change', function(){
        if ($.trim($(this).val()).length == 0) {
            $("#author_id").val('');
        }
    });
});
</script>
</body>
</html>